#include "door_interface.h"

int serial_init(char* deviceName){
  bzero(&serial_params, sizeof(serial_params));
  int fd = open(deviceName, O_RDWR | O_NOCTTY);
  if(fd < 0)//an error occured
    return -1;
  else {
    //tcgetattr(fd, &serial_params);
    serial_params.c_cflag = B19200 | CS8 | CREAD | CLOCAL | HUPCL;
    serial_params.c_iflag = IGNPAR | ICRNL;
    serial_params.c_oflag = 0; //raw output
    serial_params.c_lflag = ICANON;
    cfsetospeed(&serial_params, B19200);
    cfsetispeed(&serial_params, B19200);
    serial_params.c_cc[VINTR]    = 0;     /* Ctrl-c */
    serial_params.c_cc[VQUIT]    = 0;     /* Ctrl-\ */
    serial_params.c_cc[VERASE]   = 0;     /* del */
    serial_params.c_cc[VKILL]    = 0;     /* @ */
    serial_params.c_cc[VEOF]     = 1;     /* Ctrl-d */
    serial_params.c_cc[VTIME]    = 2;     /* inter-character timer unused */
    serial_params.c_cc[VMIN]     = 1;     /* blocking read until 1 character arrives */
    serial_params.c_cc[VSWTC]    = 0;     /* '\0' */
    serial_params.c_cc[VSTART]   = 0;     /* Ctrl-q */
    serial_params.c_cc[VSTOP]    = 0;     /* Ctrl-s */
    serial_params.c_cc[VSUSP]    = 0;     /* Ctrl-z */
    serial_params.c_cc[VEOL]     = 0;     /* '\0' */
    serial_params.c_cc[VREPRINT] = 0;     /* Ctrl-r */
    serial_params.c_cc[VDISCARD] = 0;     /* Ctrl-u */
    serial_params.c_cc[VWERASE]  = 0;     /* Ctrl-w */
    serial_params.c_cc[VLNEXT]   = 0;     /* Ctrl-v */
    serial_params.c_cc[VEOL2]    = 0;     /* '\0' */
    tcflush(fd, TCIFLUSH);
    tcsetattr(fd, TCSANOW, &serial_params);
    return fd;
  }
}


int serial_reset(int fd, char* deviceName){
  close(fd);
  return serial_init(deviceName);
}


char * serial_readln(int fd, char* buffer, int buffer_size){
  //return fscanf((FILE *)fd,"%s\r\n",buffer);
  //no can do :) device not formatted, syscalls only
  int index = 0;
  char current;
    printf("\t\t\t\t%d:",read(fd,&current,1));
  //loop until you hit newline
  while((current != '\n') && (index<buffer_size)){
    buffer[index++] = current;
    //printf("\t\t\t\t%d:",read(fd,&current,1));
    //printf("\t%c\n",current);
   }
  //strip trailing \r
  buffer[index] = '\0';
  char tmp[255];
  read(fd,&tmp,255);
  //printf("\t\tread: %s\n", buffer);
  return buffer;
}

int door_open(int fd, int timeout){
  char buff[256];
  sprintf(buff,"P:%d\r\n", timeout);

  return write(fd,&buff,strlen(buff));
    //  sleep(2);
    //;serial_readln(fd,buff,256);
    //;if( strcmp(buff,"OK") == 0 )
    //  return 0;
    //else if(strcmp(buff,"ERROR") == 0)
    //  return -2;
    //else
    //  return -3;
    //}
    //else
    //return -1;
}


int door_close(int fd){
  char buff[256];
  sprintf(buff,"C");
  //TODO:refactor this to handle ERROR return values
  if( 0 < write(fd,&buff,strlen(buff))){
    sleep(2);
    serial_readln(fd,buff,256);
    if( strcmp(buff,"OK") == 0 )
      return 0;
    else if( strcmp(buff,"ERROR") == 0  )
      return -2;
    else
      return -3;
  }
  else
    return -1;
}
//test echo function - used only to test .so functionality from scheme
int door_test(int i){
  return i;
}


char * door_string(char * buffer, int i){
  sprintf(buff_scheme, "%s: %d", buffer, i);
  return buff_scheme;
}


char * serial_readln_scheme(int fd){
  //return fscanf((FILE *)fd,"%s\r\n",buffer);
  //no can do :) device not formatted, syscalls only
  int index = 0;
  char current;
  read(fd,&current,1);
  //loop until you hit newline
  while((current != '\n') && (index<256)){
    buff_scheme[index++] = current;
    read(fd,&current,1);
    //printf("\t%c\n",current);
   }
  //strip trailing \r
  buff_scheme[index] = '\0';
  char tmp[255];
  read(fd,&tmp,255);
  printf("\t\tread: %s\n", buff_scheme);
  return buff_scheme;
}

int serial_flush(int fd){
  char tmp[1024];
  return read(fd,&tmp,1024);
}
